package Solutions


func getDistances(arr []int) []int64 {
    var ans1 = make([]int64,len(arr))
    count := make(map[int]int)
    lastInd := make(map[int]int)
    for i := 0;i < len(arr);i++{
        if v,ok:=count[arr[i]];ok{
            t:=lastInd[arr[i]]
            ans1[i]+=ans1[t]+int64(v*(i-t))
            count[arr[i]]++
            lastInd[arr[i]]=i
        }else{
            count[arr[i]]=1
            lastInd[arr[i]]=i
        }
    }
    var ans=make([]int64,len(arr))
    count = map[int]int{}
    lastInd = map[int]int{}
    for i:=len(arr)-1;i>=0;i--{
        if v,ok:=count[arr[i]];ok{
            t:=lastInd[arr[i]]
            ans[i]+=ans[t]+int64(v*(t-i))
              count[arr[i]]++
            lastInd[arr[i]]=i
        }else{
            count[arr[i]]=1
            lastInd[arr[i]]=i
        }
    }
    for i:=0;i<len(ans);i++{
        ans[i]+=ans1[i]
    }
    return ans
}